package com.yex.service.modify.order;

import com.yex.common.exception.RRException;
import com.yex.dao.UserOrderDao;
import com.yex.entity.MerchantOrderEntity;
import com.yex.entity.MessageEntity;
import com.yex.entity.UserEntity;
import com.yex.entity.UserOrderEntity;
import com.yex.service.MessageService;
import com.yex.service.modify.BaseService;
import com.yex.util.CommonModifyUtil;
import com.yex.utils.Utils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SmsService extends BaseService<UserOrderDao, UserOrderEntity> {

    @Autowired
    private MessageService messageService;

    @Autowired
    private UserOrderService userOrderService;

    @Autowired
    private MerchantOrderService merchantOrderService;

    public static final Integer EXPIRE_TIME = 60 * 1000 * 5;

    public static final Integer SEND_EXPIRE = 30 * 1000;

    private Integer _getTemplateId(Integer smsType, UserEntity userEntity, UserOrderEntity userOrderEntity, MerchantOrderEntity merchantOrderEntity) {
        switch (smsType) {
            case 10001:
                if (userOrderEntity.getStatus().compareTo(0) != 0 || merchantOrderEntity.getOrderType() != 2) {
                    throw new RRException("无效的订单信息");
                }
                if (userEntity.getId().compareTo(userOrderEntity.getUserId()) != 0) throw new RRException("无效的订单信息");
                return 34636;
            case 10002:
                if (userOrderEntity.getStatus().compareTo(1) != 0 || merchantOrderEntity.getOrderType() != 2) {
                    throw new RRException("无效的订单信息");
                }
                if (userEntity.getId().compareTo(merchantOrderEntity.getUserId()) != 0)
                    throw new RRException("无效的订单信息");
                return 34639;
            case 10003:
                if (userOrderEntity.getStatus().compareTo(0) != 0 || merchantOrderEntity.getOrderType() != 1) {
                    throw new RRException("无效的订单信息");
                }
                if (userEntity.getId().compareTo(userOrderEntity.getUserId()) != 0) throw new RRException("无效的订单信息");
                return 34640;
            case 10004:
                if (userOrderEntity.getStatus().compareTo(1) != 0 || merchantOrderEntity.getOrderType() != 1) {
                    throw new RRException("无效的订单信息");
                }
                if (userEntity.getId().compareTo(merchantOrderEntity.getUserId()) != 0)
                    throw new RRException("无效的订单信息");
                return 34641;
            default:
                throw new RRException("无效的短信类型");
        }
    }

    public void sendSms(Integer userOrderId, Integer smsType) {
        UserEntity userEntity = validateLoginUser(CommonModifyUtil.getRequest());
        UserOrderEntity userOrderEntity = userOrderService.findById(userOrderId);
        MerchantOrderEntity merchantOrderEntity = merchantOrderService.findById(userOrderEntity.getOrderId());
        Integer templateId = _getTemplateId(smsType, userEntity, userOrderEntity, merchantOrderEntity);

        MessageEntity messageEntity = messageService.selectByMobileAndType(userEntity.getMobile(), templateId, userOrderId);
        if (merchantOrderEntity == null) {
            String messageCode = Utils.randomInteger(4);
            messageEntity = new MessageEntity();
            messageEntity.setUserOrderId(userOrderId);
            messageEntity.setUserId(userEntity.getId());
            messageEntity.setType(smsType);
            messageEntity.setCreateTime(new Date());
            messageEntity.setStatus(0);
            messageEntity.setMobile(userEntity.getMobile());

            messageEntity.setCode(messageCode);
            messageEntity.setTemplateId(String.valueOf(templateId));
            Integer messageId = messageService.insertOrderMessage(messageEntity);
            messageEntity.setId(messageId);
        } else {
            Long expire = (messageEntity.getSendTime().getTime() + SEND_EXPIRE) - System.currentTimeMillis();
            if (expire > 0) {
                throw new RRException("请勿频繁发送短信,请" + expire + "秒后重新发送短信");
            }
        }
        messageEntity.setSendTime(new Date());
        Integer flag = messageService.sendMessage(messageEntity.getId());
        if (flag.compareTo(0) == 0) throw new RRException("发送短信失败");
    }


    public UserOrderEntity validateCode(Integer smsType, Integer code, Integer userOrderId) {
        if (code == 0) throw new RRException("请输入验证码");
        UserEntity userEntity = validateLoginUser(CommonModifyUtil.getRequest());
        UserOrderEntity userOrderEntity = userOrderService.findRelationData(userOrderId);
        Integer templateId = _getTemplateId(smsType, userEntity, userOrderEntity, userOrderEntity.getMerchantOrderEntity());
        MessageEntity messageEntity = messageService.selectByMobileAndType(userEntity.getMobile(), templateId, userOrderId);
        if (messageEntity == null) throw new RRException("验证码错误");
        if (messageEntity.getCode().equalsIgnoreCase(code.toString())) throw new RRException("验证码错误");
        return userOrderEntity;
    }

}
